#include "StdAfx.h"
#include "xhHubImgPro.h"
#include "DD_HubImgPro.h"



xhHubImgPro::xhHubImgPro()
{
	m_nWidth = 0;
    memset(&m_xImgCapParam,0,sizeof(m_xImgCapParam));
	memset(m_cImgAssBuf,0,sizeof(m_cImgAssBuf));
	m_ImgBitInfo.pImgCapParam = (void*)&m_xImgCapParam;
	m_ImgBitInfo.pImgAssParam = (void*)m_cImgAssBuf;
	m_pImgAssParam            = (zkImgAssParam*)m_cImgAssBuf;
	m_pImgAssData             = m_cImgAssBuf + sizeof(zkImgAssParam);
}

xhHubImgPro::~xhHubImgPro()
{
	UnInitialize();
}
int xhHubImgPro::UnInitialize()
{
	DDThread3::DDThdUnInit();

	m_xImgPro.UnInitialize();
	m_xImgPro2.UnInitialize();

	DD_AUTO_LOCK xLock(&m_Sect);

	return 0;
}

int xhHubImgPro::Initialize()
{
	xtHubImg::m_pImgInfo  = &m_ImgBitInfo;
	xtHubImg::m_pImgBit   = &m_xImgBit; 
	xtHubImg::m_pImgBit2  = &m_xImgBit2;

	m_xImgBit.pImgCapParam  = m_ImgBitInfo.pImgCapParam;
	m_xImgBit.pImgAssParam  = m_ImgBitInfo.pImgAssParam;
	m_xImgBit2.pImgCapParam = m_ImgBitInfo.pImgCapParam;
	m_xImgBit2.pImgAssParam = m_ImgBitInfo.pImgAssParam;

	m_xImgPro.Initialize();
	m_xImgPro2.Initialize();

	xtHubImg::Init();
	DDThread3::DDThdInit();

	return DD_ERR_SUCCESS;
}

int xhHubImgPro::SetHwnd(HWND hWnd,zkScreenMon *pScreen)
{
	return m_xImgPro.SetHwnd(hWnd,pScreen);
}
int xhHubImgPro::SetHwnd2(HWND hWnd,zkScreenMon *pScreen)
{
	xtHubImg::m_hWnd2 = hWnd;
	return m_xImgPro2.SetHwnd(hWnd,pScreen);
}
int xhHubImgPro::RegOnDrawCallBack(OnDrawCallBackFun funCallBack,void *pContext)
{
	return m_xImgPro.RegOnDrawCallBack(funCallBack,pContext);
}
int xhHubImgPro::RegOnDraw2CallBack(OnDrawCallBackFun funCallBack,void *pContext)
{
	return m_xImgPro2.RegOnDrawCallBack(funCallBack,pContext);
}
int xhHubImgPro::RegMsgCallBack(ImgProMsgCallBackFun funCallBack,void *pContext)
{
	DD_AUTO_LOCK xLock(&m_Sect);

	m_dwContext       = 0;
	m_pContext        = pContext;
	m_funMsgCallBack  = funCallBack;
	
	m_xImgPro.RegMsgCallBack(funCallBack,pContext);
	//m_xImgPro2.RegMsgCallBack(funCallBack,pContext);

	return 0;
}


int xhHubImgPro::LoadFile(char *pFileName)
{
	int nRet = DD_ERR_FAILURE;

	DD_AUTO_LOCK xLock(&m_Sect);

	if( NULL == pFileName || strlen(pFileName) < 4 )
	{
		return nRet;
	}

	xkImgCheck xCheck;
	DWORD dwBitPerSap = xCheck.CheckFileBitPerSap( pFileName );

	DWORD nImgDefine = eImage_Define_Img8;

	switch( dwBitPerSap )  
	{
	case 1:
	case 2:
	case 3:
	case 8:
		{
			nImgDefine = eImage_Define_Img8;
		}
		break;
	case 10:
	case 12:
	case 14:
	case 16:
		{
			nImgDefine = eImage_Define_Img16;
		}
		break;
	case 32:
		{
		}
		break;
	default:
		break;
	}

	if( eImage_Define_Img16 != nImgDefine )
	{
		return DD_ERR_PARAMETER;
	}

	nRet = xtHubImg::LoadFile(pFileName);
	if( DD_ERR_SUCCESS != nRet )
	{
		return nRet;
	}

	m_ImgBitInfo.nImgDefine = nImgDefine;
	m_ImgBitInfo.bAutoDelete = FALSE;

	memset(&m_xImgCapParam,0,sizeof(m_xImgCapParam));
	memset(m_cImgAssBuf,0,sizeof(m_cImgAssBuf));

	GetExDataFromFile(pFileName);
	
	m_n0 = m_xImgCapParam.n0;
	m_n1 = m_xImgCapParam.n1;
	m_n2 = m_xImgCapParam.n2;

	if( 0 <= m_n0 && m_n0 < m_n1 && m_n1 < m_n2 && m_n2 <= m_nWidth )
	{
		DDThread3::SetRunEvent();
	}
	else
	{
		nRet = DD_ERR_PARAMETER;
	}

	return nRet;
}



int xhHubImgPro::SetImgBuffer(zkImgBitInfo *pImgInfo,BOOL bShareMem,BOOL bShowImg)
{
	int nRet = DD_ERR_FAILURE;

	if( NULL == pImgInfo || eImage_Define_Img16 != pImgInfo->nImgDefine )
	{
		return DD_ERR_PARAMETER;
	}

	DD_AUTO_LOCK xLock(&m_Sect);

	do 
	{
		nRet = xtHubImg::SetImgBuffer(pImgInfo,bShareMem);
		if( DD_ERR_SUCCESS != nRet )
		{
			return nRet;
		}

		memset(&m_xImgCapParam,0,sizeof(m_xImgCapParam));
		memset(m_cImgAssBuf,0,sizeof(m_cImgAssBuf));

		if( NULL != pImgInfo->pImgCapParam )
		{
			memcpy( m_ImgBitInfo.pImgCapParam,pImgInfo->pImgCapParam,sizeof(zkImgCapParam) );
		}
		if( NULL != pImgInfo->pImgAssParam )
		{
			DWORD dwImgBugSize = ( (zkImgAssParam*)pImgInfo->pImgAssParam )->dwImgBugSize;
			DWORD dwBufferSize = dwImgBugSize + sizeof(zkImgAssParam);

			if( dwBufferSize > DEFINE_ASS_PARAM_SIZE )
			{
				return DD_ERR_FAILURE;
			}

			memcpy( m_ImgBitInfo.pImgAssParam,pImgInfo->pImgAssParam,dwBufferSize );
		}
		m_pImgAssParam->nLeftGray  = m_xImgCapParam.nLeftGray;
		m_pImgAssParam->nRightGray = m_xImgCapParam.nRightGray;

		m_pImgAssParam->nImgTechLevel  = 1;
		m_pImgAssParam->nImgConclusion = 1;

		m_n0 = m_xImgCapParam.n0;
		m_n1 = m_xImgCapParam.n1;
		m_n2 = m_xImgCapParam.n2;

		if( 0 <= m_n0 && m_n0 < m_n1 && m_n1 < m_n2 && m_n2 <= m_nWidth )
		{
			if( bShowImg )
			{
				DDThread3::SetRunEvent();
			}
			else
			{
				m_funMsgCallBack(ImagePro_Loading,100,m_dwContext,m_pContext);
			}
		}
		else
		{
			nRet = DD_ERR_PARAMETER;
		}

		return nRet;
 
	} while (false);

	return nRet;
}




int xhHubImgPro::DDThdDispatch(HANDLE hExitEvent)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	
	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(ImagePro_Loading,0,m_dwContext,m_pContext);
	}

	int nRet = DD_ERR_FAILURE;
	do 
	{
		nRet = xtHubImg::TransformHubData();
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		nRet = m_xImgPro.SetImgBuffer( &m_xImgBit );
		nRet |= m_xImgPro2.SetImgBuffer( &m_xImgBit2 );
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		nRet = 100;

	} while (FALSE);

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(ImagePro_Loading,nRet,m_dwContext,m_pContext);
	}
	
	DDThread3::SetRunEvent(false);

	return nRet;
}


int xhHubImgPro::GetDispImgSize(int &nWidth,int &nHeight)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro.GetImageSize(nWidth,nHeight);
}
int xhHubImgPro::GetDispImg2Size(int &nWidth,int &nHeight)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro2.GetImageSize(nWidth,nHeight);
}


int xhHubImgPro::Operate(DWORD dwProMode,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	
	m_xImgPro.Operate( dwProMode,pVoid );
	m_xImgPro2.Operate( dwProMode,pVoid );

	return 0;
}

int	xhHubImgPro::Draw(void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro.Draw(pVoid);
}
int	xhHubImgPro::Draw2(void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro2.Draw(pVoid);
}

int xhHubImgPro::HubImgCtrl(DWORD dwType,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);

	int nRet = 0;

	if( dwType == ImageCtrl_AssParma )
	{
		if( NULL != pVoid )
		{
			zkImgAssParam *pParam = (zkImgAssParam*)pVoid;
			int nSize = sizeof(zkImgAssParam) + pParam->dwImgExSize;
			if( nSize > DEFINE_ASS_PARAM_SIZE )
			{
				return DD_ERR_BUFFERTOOSMALL;
			}
			memcpy(m_ImgBitInfo.pImgAssParam,pVoid,nSize);
		}
	}
	else if( dwType == ImageCtrl_CapParma )
	{
		if( NULL != pVoid )
		{
			m_xImgCapParam = *( (zkImgCapParam*)pVoid );
		}
	}
	else
	{
		nRet = m_xImgPro.ImageCtrl(dwType,pVoid);
		nRet|= m_xImgPro2.ImageCtrl(dwType,pVoid);
	}

	return nRet;
}

int xhHubImgPro::SaveDispToFile(char *pFileName,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro.SaveDispToFile(pFileName,pVoid);
}
int xhHubImgPro::SaveDisp2ToFile(char *pFileName,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro2.SaveDispToFile(pFileName,pVoid);
}
int xhHubImgPro::GetHistogramInfo(zkImgHistogram **ppHistogram)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro.GetHistogramInfo(ppHistogram);
}
int xhHubImgPro::GetHistogram2Info(zkImgHistogram **ppHistogram)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro2.GetHistogramInfo(ppHistogram);
}

int xhHubImgPro::GetGrayValue(int x,int y,DWORD &dwGray)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro.GetGrayValue(x,y,dwGray);
}
int xhHubImgPro::GetGray2Value(int x,int y,DWORD &dwGray)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro2.GetGrayValue(x,y,dwGray);
}
int xhHubImgPro::GetLineGray(POINT tStart,POINT tEnd,zkImgLineGray **ppLineGray)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro.GetLineGray(tStart,tEnd,ppLineGray);
}
int xhHubImgPro::GetLine2Gray(POINT tStart,POINT tEnd,zkImgLineGray **ppLineGray)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro2.GetLineGray(tStart,tEnd,ppLineGray);
}


int xhHubImgPro::GetImageData(zkImgBitInfo **ppImgInfo)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	
	*ppImgInfo = &m_ImgBitInfo;

	return 0;
}
int xhHubImgPro::GetImageInfo(zkImgBitInfo **ppImgInfo)
{
	DD_AUTO_LOCK xLock(&m_Sect);

	*ppImgInfo = &m_ImgBitInfo;

	return 0;
}



int xhHubImgPro::SaveFile(char *pFileName,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);

	int nRet = -1;
	
	CImageTIF xImgTIF;
	if( DD_ERR_SUCCESS != (nRet = xImgTIF.SaveImgFile( pFileName, &m_ImgBitInfo ) ) )
	{
		return nRet;
	}
	xImgTIF.CloseFile();

	zkImgSaveInfo *pInfo = (zkImgSaveInfo*)pVoid;
	if( NULL != pInfo )
	{
		if( pInfo->dwSize > DEFINE_ASS_PARAM_SIZE )
		{
			return DD_ERR_BUFFERTOOSMALL;
		}
		memcpy(m_ImgBitInfo.pImgAssParam,pInfo->pParam,pInfo->dwSize);
	}

	if( DD_ERR_SUCCESS != (nRet = SaveExDataFromFile(pFileName) ) )
	{
		return nRet;
	}

	return nRet;
}

int xhHubImgPro::DrawMark(void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro.DrawMark(pVoid);
}
int xhHubImgPro::Draw2Mark(void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);
	return m_xImgPro2.DrawMark(pVoid);
}





int DD_HubImgPro::GetFileInfo(char *pFileName,zkImgFileTail *pFileInfo)
{
	int nRet = -1;

	DDReadWriteFile xFile;

	do 
	{
		LONGLONG llFileSize = 0;

		if( 0 != xFile.DDCreateFile(pFileName,TRUE,&llFileSize) )
		{
			nRet = -1;
			break;
		}

		LONGLONG llFileOffset = llFileSize - sizeof(zkImgFileTail);

		nRet = xFile.DDOperatorFile( (BYTE*)pFileInfo,sizeof(zkImgFileTail),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		if( !pFileInfo->CheckVersion() )
		{
			nRet = -1;
			break;
		}

	}while(false);

	return nRet;
}

int DD_HubImgPro::GetFileExInfo(char *pFileName,zkImgFileTail *pFileInfo,zkImgCapParam *pCapParam,zkImgAssParam *pAssParam)
{
	int nRet = -1;

	if( NULL == pFileName || NULL == pFileInfo || NULL == pCapParam || NULL == pAssParam )
	{
		return -1;
	}

	DDReadWriteFile xFile;

	do 
	{
		LONGLONG llFileSize = 0;

		if( 0 != xFile.DDCreateFile(pFileName,TRUE,&llFileSize) )
		{
			nRet = -1;
			break;
		}

		LONGLONG llFileOffset = llFileSize - sizeof(zkImgFileTail);

		nRet = xFile.DDOperatorFile( (BYTE*)pFileInfo,sizeof(zkImgFileTail),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		if( !pFileInfo->CheckVersion() )
		{
			nRet = -1;
			break;
		}

		llFileOffset = pFileInfo->dwExDataPos;
		nRet = xFile.DDOperatorFile( (BYTE*)pCapParam,sizeof(zkImgCapParam),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		llFileOffset = pFileInfo->dwExDataPos + sizeof(zkImgCapParam);
		nRet = xFile.DDOperatorFile( (BYTE*)pAssParam,sizeof(zkImgAssParam),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		if( pAssParam->dwImgExSize > DEFINE_ASS_PARAM_SIZE - sizeof(zkImgAssParam) )
		{
			nRet = DD_ERR_BUFFERTOOSMALL;
			break;
		}

		if( pAssParam->dwImgExSize > 0 )
		{
			BYTE *pImgAssData = (BYTE*)pAssParam + sizeof(zkImgAssParam);
			llFileOffset = pFileInfo->dwExDataPos + sizeof(zkImgCapParam) + sizeof(zkImgAssParam);
			nRet = xFile.DDOperatorFile( pImgAssData,pAssParam->dwImgExSize,&llFileOffset);
			if( nRet != 0)
			{
				break;
			}
		}

	}while(false);

	return nRet;
}



int xhHubImgPro::GetExDataFromFile(char *pFileName)
{
	int nRet = -1;

	do 
	{
		LONGLONG llFileSize = 0;

		if( 0 != m_xFile.DDCreateFile(pFileName,TRUE,&llFileSize) )
		{
			nRet = -1;
			break;
		}

		LONGLONG llFileOffset = llFileSize - sizeof(zkImgFileTail);

		zkImgFileTail xFileTail;
		nRet = m_xFile.DDOperatorFile( (BYTE*)&xFileTail,sizeof(zkImgFileTail),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		if( !xFileTail.CheckVersion() )
		{
			nRet = -1;
			break;
		}

		llFileOffset = xFileTail.dwExDataPos;
		nRet = m_xFile.DDOperatorFile( (BYTE*)&m_xImgCapParam,sizeof(zkImgCapParam),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		llFileOffset = xFileTail.dwExDataPos + sizeof(zkImgCapParam);
		nRet = m_xFile.DDOperatorFile( (BYTE*)m_pImgAssParam,sizeof(zkImgAssParam),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		if( m_pImgAssParam->dwImgExSize > DEFINE_ASS_PARAM_SIZE - sizeof(zkImgAssParam) )
		{
			nRet = DD_ERR_BUFFERTOOSMALL;
			break;
		}

		if( m_pImgAssParam->dwImgExSize > 0 )
		{
			llFileOffset = xFileTail.dwExDataPos + sizeof(zkImgCapParam) + sizeof(zkImgAssParam);
			nRet = m_xFile.DDOperatorFile(m_pImgAssData,m_pImgAssParam->dwImgExSize,&llFileOffset);
			if( nRet != 0)
			{
				break;
			}
		}

	} while (FALSE);

	m_xFile.DDCloseFile();

	return nRet;
}

int xhHubImgPro::SaveExDataFromFile(char *pFileName)
{
	int nRet = -1;

	do 
	{
		LONGLONG llFileSize = 0;

		if( 0 != m_xFile.DDCreateFile(pFileName,FALSE,&llFileSize) )
		{
			nRet = -1;
			break;
		}
		zkImgFileTail xFileTail;
		xFileTail.dwExDataPos = (DWORD)llFileSize;

		xFileTail.xFileInfo.nImgTechLevel  = (unsigned char)m_pImgAssParam->nImgTechLevel;
		xFileTail.xFileInfo.nImgConclusion = (unsigned char)m_pImgAssParam->nImgConclusion;

		if( strlen(m_pImgAssParam->szVerifyUser) > 0 )
		{
			xFileTail.xFileInfo.nVerifyResult = 1;
		}

		DWORD    dwExDataSize = sizeof(zkImgCapParam);
		LONGLONG llFileOffset = llFileSize;

		nRet = m_xFile.DDOperatorFile( (BYTE*)&m_xImgCapParam,sizeof(zkImgCapParam),&llFileOffset,FALSE);
		if( nRet != 0)
		{
			break;
		}

		DWORD dwAssParamSize = sizeof(zkImgAssParam) + m_pImgAssParam->dwImgExSize;
		dwExDataSize += dwAssParamSize;

		llFileOffset = llFileSize + sizeof(zkImgCapParam);
		nRet = m_xFile.DDOperatorFile( (BYTE*)m_pImgAssParam,dwAssParamSize,&llFileOffset,FALSE);
		if( nRet != 0)
		{
			break;
		}

		xFileTail.dwExDataSize = dwExDataSize;

		llFileOffset = llFileSize + dwExDataSize;
		nRet = m_xFile.DDOperatorFile( (BYTE*)&xFileTail,sizeof(zkImgFileTail),&llFileOffset,FALSE);
		if( nRet != 0)
		{
			break;
		}

	} while (FALSE);

	m_xFile.DDCloseFile();

	return nRet;
}